package 链表和树.链表;
//Java：删除链表的倒数第 N 个结点
public class RemoveNthNodeFromEndOfList{
    public static void main(String[] args) {
        Solution solution = new RemoveNthNodeFromEndOfList().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
//    public class ListNode {
//        int val;
//        ListNode next;
//        ListNode() {}
//        ListNode(int val) { this.val = val; }
//        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
//    }
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //1.方法1遍历链表长度减去n就是要删除节点n的前一个位置

        //2.方法2有dummy使用两个指针都在dummy先让前一个指针走n步那么后两个再一起走始终都是n个滑动，当前一个到尾则后一个到n前一个了
        ListNode dummy = new ListNode(0,head);
        ListNode right = dummy;
        ListNode left = dummy;
        while(n!= 0){
            right = right.next;
            n--;
        }
        while (right.next!= null){
            right= right.next;
            left = left.next;
        }
        left.next = left.next.next;
        return dummy.next;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}